package uk.ac.glasgow.demosocs.impl.ui;

import java.util.Set;

import uk.ac.glasgow.clui.CommandDescription;

import uk.ac.glasgow.clui.ArgumentsDescription;
import uk.ac.glasgow.clui.MinimumArguments;
import uk.ac.glasgow.clui.SystemCommand;
import uk.ac.glasgow.clui.SystemCommandException;
import uk.ac.glasgow.demosocs.Candidate;
import uk.ac.glasgow.demosocs.Election;
import uk.ac.glasgow.demosocs.ElectionState;
import uk.ac.glasgow.demosocs.VotingSystem;
import uk.ac.glasgow.demosocs.impl.ElectionImpl;
import uk.ac.glasgow.demosocs.impl.OneOfNVoteStub;
import uk.ac.glasgow.demosocs.impl.RankedVoteImpl;
import uk.ac.glasgow.demosocs.impl.ReferendumVoteImpl;

@MinimumArguments(1)
@CommandDescription("Cast a vote in the currently selected election for the specified candidate.")
@ArgumentsDescription("<candidate>")
public class CastVoteCommand extends SystemCommand<VotingSystem> {

	public CastVoteCommand(VotingSystem facade) {
		super(facade);
	}

	@Override
	public String processCommand(String[] arguments) throws SystemCommandException {

		String guid = facade.getCurrentUserGUID();
		if (guid == null)
			throw new SystemCommandException("No current user set.");

		Election election = facade.getCurrentElection();
		ElectionImpl e = ((ElectionImpl) election);

		if (election == null)
			throw new SystemCommandException("No current election set.");

		if (!election.getElectionState().equals(ElectionState.STARTED))
			throw new SystemCommandException("Election is not open");

		// Fast past the post or referendum
		if (e.getElectionType().equals("Simple_Plurality")) {
			String candidate = arguments[0];
			facade.castVote(new OneOfNVoteStub(candidate));
			return "";
		}

		else if (e.getElectionType().equals("Alternative_Vote")) {
			facade.castVote(new RankedVoteImpl(arguments));
			return "";
		}
		else if (e.getElectionType().equals("Referendum")) {
			String option = arguments[0];
			facade.castVote(new ReferendumVoteImpl(option));
			return "";
		}
    return "";
	}
}
